package com.sx.user.controller;

import com.sx.code.CodeDubboService;
import com.sx.user.UserDubboService;
import com.sx.user.entity.Token;
import com.sx.user.entity.User;
import com.sx.user.service.RegisterService;
import com.sx.user.utils.TokenUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;


/**
 * @author shengx
 * @date 2020/5/9 23:49
 */
//@RestController
//@RequestMapping("/user")
@RefreshScope
@Service
public class UserController implements UserDubboService {
    @Value("${test}")
    private String test;
    @Reference
    private CodeDubboService codeDubboService;
    @Autowired
    private RegisterService registerService;

    /**
     * 登录接⼝，验证⽤户名密码合法性，根据⽤户名和密码⽣成token，token存⼊数据库，并写⼊cookie中，登录成功返回邮箱地址，重定向到欢迎⻚
     * @param email
     * @param password
     * @return
     */
//    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password) {
        System.out.println("email:" + email + " password:" + password);
        User user = registerService.signIn(email, password);
        if (user != null) {
            String token = TokenUtils.getToken(email);
            Token myToken = registerService.addToken(email, token);
//            Cookie cookie = new Cookie("token", myToken.getToken());
//            cookie.setPath("/");
            return myToken.getToken();
        }
        return null;
    }



    /**
     * 根据token查询⽤户登录邮箱接⼝
     * @param token
     * @return
     */
//    @GetMapping("/info/{token}")
    public String info(@PathVariable String token) {
        System.out.println("token:" + token);
        Token myToken = registerService.getEmailByToken(token);
        System.out.println(myToken);
        return myToken != null ? myToken.getEmail() : "";
    }

    /**
     * 是否已注册，根据邮箱判断,true代表已经注册过，false代表尚未注册
     * @param email
     * @return
     */
//    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email) {
        System.out.println("email:" + email);
        return registerService.isRegister(email);
    }

    /**
     * 注册接⼝，true成功，false失败
     * @param email
     * @param password
     * @param code
     * @return
     */
//    @GetMapping("/register/{email}/{password}/{code}")
    public Boolean register(@PathVariable String email, @PathVariable String password, @PathVariable String code) {
        System.out.println("email:" + email + " password:" + password + " code" + code);
        Integer validate = codeDubboService.validate(email, code);
        int success = -1;
        if (validate == 0) {
            success = registerService.register(email, password);
        }
        if (success > 0) {
            return true;
        }
        return false;
    }

//    @GetMapping("/test")
    public String test() {

        return this.test;
    }
}
